package com.fept.msgcore.msgserver.protocol.util.crypto;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * Created by Srma on 2/9/2017.
 */
public class MACUtil {
    public MACUtil() {
    }

    public static byte[] hamc(String alg, byte[] macKey, byte[] macData) throws NoSuchAlgorithmException, InvalidKeyException {
        if(macKey != null && macData != null) {
            Mac mac = Mac.getInstance(alg);
            SecretKeySpec secret = new SecretKeySpec(macKey, alg);
            mac.init(secret);
            byte[] doFinal = mac.doFinal(macData);
            return doFinal;
        } else {
            return null;
        }
    }

    public static byte[] hmac(String alg, String key, String data) throws InvalidKeyException, NoSuchAlgorithmException {
        return hamc(alg, key.getBytes(), data.getBytes());
    }

    public static byte[] hmac(String alg, String key, byte[] data) throws InvalidKeyException, NoSuchAlgorithmException {
        return hamc(alg, key.getBytes(), data);
    }
}
